SID = 0;
PhotoUploader = [];
getPhotoUploader = function(ID) {
	return PhotoUploader[ID];
};

createObjID = function() {
	var id = SID;
	SID++;
	return id;
}

var uploader = function(uploader) {
	this.id = uploader.id;
	this.albumBoxId = uploader.albumBoxId;
	this.SID = createObjID();
	PhotoUploader[this.SID] = this;
	this.config = {};
	$.extend(this.config, {
		file_upload_limit : 30,
		file_size_limit : "8 MB"
	});
	$.extend(this.config, uploader.config);
	this.init();
};

uploader.prototype = {
	init:function() {
		this.swfObj = new SWFUpload(this.getSetting());
		this.fileCount = 0;
		this.fileIds = [];
		this.fileTotalSize = 0;
		this.uploadedCount = 0;
		this.toUpAlbumId = "";
		this.uploadedSize = 0;
		this.uploadedCount = 0;
		this.getEl("files-remain")[0].innerHTML = this.getConfig("file_upload_limit");
		this.getEl("file-limit")[0].innerHTML = this.getConfig("file_upload_limit");
		this.getEl("clear-list").hide();
		this.bindEvents();
	},
	
	getSetting:function() {
		var This = this;
		var setting = {
				debug : DEBUG_MODE,
				flash_url : SWFUPLOAD_FLASH,
				upload_url : UPLOAD_URL,
				file_post_name : This.getConfig("file_post_name"),
				post_params : {},
				file_size_limit : This.getConfig("file_size_limit"),
				file_types : "*.jpg;*.jpeg;*.png;*.bmp;*.gif",
				file_types_description : "All Image Files",
				file_upload_limit : This.getConfig("file_upload_limit"),
				file_queue_limit : 0,
				custom_settings : {
					progressTarget : "flashUploaderHolder",
					cancelButtonId : "btnCancel"
				},
				button_placeholder_id : This.getId(This.getConfig("handler")),
				button_image_url : This.getConfig("button_image_url"),
				button_width : 100,
				button_height : 24,
				button_text_style : ".link1 {font-family:tahoma,simsun;font-size:14px;color:#005eac;font-weight:bold;}",
				button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES,
				button_cursor : SWFUpload.CURSOR.HAND,
				button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT,
				swfupload_pre_load_handler : function() {
					This.swfUploadPreLoad(this);
				},
				swfupload_loaded_handler : function() {
					This.swfUploadLoaded(this);
				},
				swfupload_load_failed_handler : function() {
					This.swfUploadLoadFailed(this);
				},
				file_queued_handler : function(file) {
					This.fileQueued(this, file);
				},
				file_queue_error_handler : function(file, errcode, msg) {
					This.fileQueueError(this, file, errcode, msg);
				},
				file_dialog_complete_handler : function() {
					This.fileDialogComplete(this);
				},
				file_dialog_start_handler : function() {
					This.fileDialogStart(this);
				},
				upload_start_handler : function(file) {
					This.uploadStart(this, file);
				},
				upload_progress_handler : function(file, complete, total) {
					This.uploadProgress(this, file, complete, total);
				},
				upload_error_handler : function(file, errcode, msg) {
					This.uploadError(this, file, errcode, msg);
				},
				upload_success_handler : function(file, serverdata, response) {
					This.uploadSuccess(this, file, serverdata, response);
				},
				upload_complete_handler : function(file) {
					This.uploadComplete(this, file);
				},
				queue_complete_handler : function() {
					This.queueComplete(this);
				},
				minimum_flash_version : "9.0.28"
			};
			return setting;
	},
	
	bindEvents:function() {
		var This = this;
		this.getEl("clear-list").click(function(e){
			This.clearList();
		});
		this.getEl("submit").click(function(e) {
//			XN.event.stop(e || window.event);
			This.startUpload();
		});
	},
	
	updateUI : function() {
		if (this.fileCount > 0
				&& this.getEl("clear-list:hidden")) {
			this.getEl("clear-list").show();
			this.getEl("submit")[0].disabled = false;
		} else {
			if (this.fileCount == 0) {
				this.getEl("clear-list").hide();
				this.getEl("submit")[0].disabled = true;
			}
		}
		this.getEl("files-count")[0].innerHTML = this.fileCount;
		this.getEl("files-count2")[0].innerHTML = this.fileCount;
		this.getEl("files-remain")[0].innerHTML = this.getConfig("file_upload_limit") - this.fileCount;
		this.getEl("files-total-size")[0].innerHTML = this.getSizeInKMG(this.fileTotalSize);
	},
	
	removeInQueued : function(fileid) {
		var file = this.swfObj.getFile(fileid);
		this.fileCount--;
		this.fileTotalSize -= file.size;
		var i = 0;
		while (this.fileIds[i]) {
			if (fileid === this.fileIds[i]) {
				this.fileIds.splice(i, 1);
				break;
			}
			i++;
		}
		this.swfObj.cancelUpload(fileid);
		this.getEl(fileid).remove();
		this.updateUI();
	},
	
	clearList : function() {
		var _10 = this;
		var i = 0;
		while (this.fileIds[i]) {
			var fileid = this.fileIds[i];
			this.getEl(fileid).remove();
			this.swfObj.cancelUpload(fileid);
			i++;
		}
		this.fileIds.length = 0;
		this.fileCount = 0;
		this.fileTotalSize = 0;
		this.updateUI();
		this.getEl("submit")[0].disabled = true;
	},

	getId:function(suffix) {
		return this.id + "-" + suffix;
	},
	
	getSID : function() {
		return this.SID;
	},
	
	getConfig : function(key) {
		return this.config[key];
	},
	
	getAlbumBoxEl : function(suffix) {
		return $('#'+this.albumBoxId + "-" + suffix);
	},
	getPath : function(url) {
		return DOMAIN + url;
	},
	getEl : function(suffix) {
		return $('#'+this.getId(suffix));
	},
	getSizeInKMG : function(num) {
		if (isNaN(num)) {
			return false;
		}
		num = parseInt(num);
		var bs = [ " B", " KB", " MB", " GB" ];
		for ( var i = 0; i < bs.length; i += 1) {
			if (num < 1024) {
				num = num + "";
				if (num.indexOf(".") != -1 && num.indexOf(".") != 3) {
					num = num.substring(0, 4);
				} else {
					num = num.substring(0, 3);
				}
				break;
			} else {
				num = num / 1024;
			}
		}
		return num + bs[i];
	},
	swfUploadPreLoad : function(This) { },
	swfUploadLoaded : function(This) { },
	swfUploadLoadFailed : function() {
		alert("您需要安装Flash Player 9以上版本才能使用多文件上传，即将为您切换到普通方式。");
		window.location = getPath("/views/batchupload.jsp");
	},
	fileQueued : function(This, file) {
		this.fileCount++;
		this.fileIds.push(file.id);
		this.fileTotalSize += file.size;
		this.moveBtn();
		var flist = this.getEl("files-list");
		var tr = document.createElement('tr');
		tr.id = this.getId(file.id);
		var td1 = document.createElement('td');
		td1.innerHTML = file.name;
		var td2 = document.createElement('td');
		td2.innerHTML = this.getSizeInKMG(file.size);
		var td3 = document.createElement('td');
		td3.className = "remove";
		td3.innerHTML = [
				"<a href=\"#nogo\" onclick=\"getPhotoUploader(" + this.getSID()
						+ ").removeInQueued('" + file.id + "')\">",
				"删除</a>" ].join("");
		$(tr).append(td1);
		$(tr).append(td2);
		$(tr).append(td3);
		flist.append(tr);
		this.updateUI();
		if (this.getEl("submit")[0].disabled) {
			this.getEl("submit")[0].disabled = false;
		}
		//this.fileQueued();
	},
	fileQueueError : function(This, file, errcode, msg) {
		try {
			if (errcode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
				var tip = [
						"你选择的照片太多了，",
						"一次只能上传"
								+ this.getConfig("file_upload_limit")
								+ "张照片<br/>" ];
				alert(tip.join(""));
				return;
			}
			if (errcode == SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT) {
				alert(file.name + "文件过大，请保证每张图片在8M以内。");
			}
			switch (errcode) {
			case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				this.swfObj.debug("Error Code: File too big, File name: "
						+ file.name + ", File size: " + file.size + ", Message: "
						+ msg);
				break;
			case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				this.swfObj.debug("Error Code: Zero byte file, File name: "
						+ file.name + ", File size: " + file.size + ", Message: "
						+ msg);
				break;
			case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
				this.swfObj.debug("Error Code: Invalid File Type, File name: "
						+ file.name + ", File size: " + file.size + ", Message: "
						+ msg);
				break;
			default:
				this.swfObj.debug("Error Code: " + errcode + ", File name: "
						+ file.name + ", File size: " + file.size + ", Message: "
						+ msg);
				break;
			}
		} catch (ex) {
			this.swfObj.debug(ex);
		}
	},
	fileDialogComplete : function(This) { },
	fileDialogStart : function(This) { },
	uploadStart : function(This, file) { },
	uploadProgress : function(This, file, serverdata, response) { },
	uploadError : function(This, file, errcode, msg) {
		try {
			switch (errcode) {
			case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
				this.swfObj.debug("Error Code: HTTP Error, File name: "
						+ file.name + ", Message: " + msg);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
				this.swfObj.debug("Error Code: Upload Failed, File name: "
						+ file.name + ", File size: " + file.size + ", Message: "
						+ msg);
				break;
			case SWFUpload.UPLOAD_ERROR.IO_ERROR:
				this.swfObj.debug("Error Code: IO Error, File name: "
						+ file.name + ", Message: " + msg);
				break;
			case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
				this.swfObj.debug("Error Code: Security Error, File name: "
						+ file.name + ", Message: " + msg);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				this.swfObj
						.debug("Error Code: Upload Limit Exceeded, File name: "
								+ file.name + ", File size: " + file.size
								+ ", Message: " + msg);
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
				this.swfObj
						.debug("Error Code: File Validation Failed, File name: "
								+ file.name
								+ ", File size: "
								+ file.size
								+ ", Message: " + msg);
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				this.swfObj.debug("Error Code: " + errcode + ", File name: "
						+ file.name + ", File size: " + file.size + ", Message: "
						+ msg);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				this.swfObj.debug("Error Code: " + errcode + ", File name: "
						+ file.name + ", File size: " + file.size + ", Message: "
						+ msg);
				break;
			default:
				this.swfObj.debug("Error Code: " + errcode + ", File name: "
						+ file.name + ", File size: " + file.size + ", Message: "
						+ msg);
				break;
			}
			if (file && file.id && $("#"+file.id + "progress")) {
				var e = $("#"+"status_" + file.id)[0];
				if (e) {
					e.innerHTML = "失败";
				}
			}
		} catch (ex) {
			this.swfObj.debug(ex);
		}
	},
	
	moveBtn : function() {
		if (this.btnMoved) {
			return;
		}
		this.swfObj.setButtonImageURL(this.getPath("images/upload2-button.png"));
		this.getEl("files-box").show();
		this.getEl("submit-box").show();
		this.getEl("files").show();
		this.getEl("file-limit").parent().show();
		this.getEl("files-count2").parent().show();
		this.getEl("hander-box").addClass("selected");
		this.btnMoved = true;
	},
	startUpload : function() {
		if (this.fileCount == 0) {
			alert("请先选择照片 :)");
			return;
		}
		this.toUpAlbumId = this.getAlbumBoxEl("album-list")[0].value;
		if (this.getAlbumBoxEl("album-list").options
				&& this.getAlbumBoxEl("album-list")[0].options.length == 0) {
			alert("您还没有相册，请先创建。");
			return;
		}
		if (this.toUpAlbumId == '') {
			alert("请选择相册");
			return;
		}
		this.getEl("files-count2").parent().hide();
		this.getEl("files-list").parent().hide();
		this.getEl("file-limit").parent().hide();
		this.getEl("files").hide();
		this.getEl("submit-box").hide();
		this.getEl("submit")[0].disabled = true;
		this.getEl("uploading-box").hide();
		this.getEl("uploaded-remain")[0].innerHTML = this.fileCount;
		if (this.getEl("progress")) {
			
		}
		this.swfObj.setUploadURL(UPLOAD_URL + "?albumid=" + this.toUpAlbumId);
		this.swfObj.setButtonDimensions(0, 0);
		this.swfObj.setButtonDisabled(true);
		this.swfObj.startUpload();
		window.scrollTo(0, 0);
	},
	uploadSuccess : function(This, file, serverdata, response) {
		this.uploadedCount++;
		this.uploadedSize += file.size;
		this.swfObj.debug("上传成功的文件:"
				+ file.name + "      文件大小:"
				+ this.uploadedSize);
		var size = Math.round(this.uploadedSize * 100 / this.fileTotalSize);
		if (this.getEl("progress")) {
			
		} else {
			//var el = XN.element.getElementsByClassName("progress", this
			//		.getEl("uploading-box"), "p")[0];
			//el.innerHTML = _4d + "%";
		}
	},
	uploadComplete : function(This, file) {
		if (This.getStats().files_queued === 0) {
			alert("uploadedOk");
		} else {
			this.swfObj.startUpload();
		}
	},
	queueComplete : function(This) { }
}
/*
var swfSetting = {
	debug : DEBUG_MODE,
	flash_url : SWFUPLOAD_FLASH,
	upload_url : UPLOAD_URL,
	file_post_name : config.file_post_name,
	post_params : {},
	file_size_limit : config.file_size_limit,
	file_types : "*.jpg;*.jpeg;*.png;*.bmp;*.gif",
	file_types_description : "All Image Files",
	file_upload_limit : config.file_upload_limit,
	file_queue_limit : 0,
	custom_settings : {
		progressTarget : "flashUploaderHolder",
		cancelButtonId : "btnCancel"
	},
	button_placeholder_id : config.handler,
	button_image_url : config.button_image_url,
	button_width : 100,
	button_height : 24,
	button_text_style : ".link1 {font-family:tahoma,simsun;font-size:14px;color:#005eac;font-weight:bold;}",
	button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES,
	button_cursor : SWFUpload.CURSOR.HAND,
	button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT,
	swfupload_pre_load_handler : function() { },
	swfupload_loaded_handler : function() { },
	swfupload_load_failed_handler : function() { },
	file_queued_handler : function(file) { },
	file_queue_error_handler : function(file, errcode, msg) { },
	file_dialog_complete_handler : function() { },
	file_dialog_start_handler : function() { },
	upload_start_handler : function(file) { },
	upload_progress_handler : function(file, complete, total) { },
	upload_error_handler : function(file, errcode, msg) { },
	upload_success_handler : function(file, serverdata, response) { },
	upload_complete_handler : function(file) { },
	queue_complete_handler : function() { },
	minimum_flash_version : "9.0.28"
};*/